# Copyright (c) Facebook, Inc. and its affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

load("//tools/build_defs/oss:yoga_defs.bzl", "ANDROID", "CXX_LIBRARY_WHITELIST", "JNI_TARGET", "JSR_305_TARGET", "JUNIT_TARGET", "PROGUARD_ANNOTATIONS_TARGET", "SOLOADER_TARGET", "YOGA_ROOTS", "yoga_android_dep", "yoga_cxx_library", "yoga_dep", "yoga_java_binary", "yoga_java_library", "yoga_java_test", "yoga_prebuilt_cxx_library")

CXX_LIBRARY_WHITELIST_FOR_TESTS = CXX_LIBRARY_WHITELIST + [
    yoga_android_dep("testutil:jni"),
    yoga_android_dep("testutil:testutil-jni"),
]

YOGA_JAVA_IMPLEMENTATION_FILES = [
    "com/facebook/yoga/*JNI*.java",
    "com/facebook/yoga/*Factory.java",
    "com/facebook/yoga/YogaNative.java",
]

yoga_prebuilt_cxx_library(
    name = "ndklog",
    exported_platform_linker_flags = [
        (
            "^android.*",
            ["-llog"],
        ),
    ],
    header_only = True,
    visibility = YOGA_ROOTS,
)

yoga_cxx_library(
    name = "jni",
    srcs = glob(["jni/*.cpp"]),
    header_namespace = "yoga/java",
    exported_headers = glob(["jni/*.h"]),
    allow_jni_merging = True,
    compiler_flags = [
        "-fno-omit-frame-pointer",
        "-fexceptions",
        "-fvisibility=hidden",
        "-ffunction-sections",
        "-fdata-sections",
        "-fPIC",
        "-Wall",
        "-Werror",
        "-Os",
        "-std=c++11",
    ],
    platforms = ANDROID,
    preprocessor_flags = [
        "-DFBJNI_WITH_FAST_CALLS",
    ],
    soname = "libyoga.$(ext)",
    visibility = ["PUBLIC"],
    deps = [
        JNI_TARGET,
        yoga_dep(":yoga-static"),
        ":ndklog",
    ],
)

yoga_java_library(
    name = "java-interface",
    srcs = glob(
        ["com/facebook/yoga/*.java"],
        exclude = YOGA_JAVA_IMPLEMENTATION_FILES,
    ),
    required_for_source_only_abi = True,
    source = "1.7",
    target = "1.7",
    visibility = ["PUBLIC"],
    deps = [
        JSR_305_TARGET,
        PROGUARD_ANNOTATIONS_TARGET,
    ],
)

yoga_java_library(
    name = "java-impl",
    srcs = glob(YOGA_JAVA_IMPLEMENTATION_FILES),
    required_for_source_only_abi = True,
    source = "1.7",
    target = "1.7",
    deps = [
        ":java-interface",
        ":jni",
        JSR_305_TARGET,
        PROGUARD_ANNOTATIONS_TARGET,
        SOLOADER_TARGET,
    ],
)

yoga_java_library(
    name = "java",
    required_for_source_only_abi = True,
    source = "1.7",
    target = "1.7",
    tests = [
        yoga_dep("java:tests"),
    ],
    visibility = ["PUBLIC"],
    exported_deps = [
        ":java-impl",
        ":java-interface",
    ],
)

yoga_java_test(
    name = "tests",
    srcs = glob(["tests/**/*.java"]),
    contacts = ["oncall+yoga@xmail.facebook.com"],
    cxx_library_whitelist = CXX_LIBRARY_WHITELIST_FOR_TESTS,
    use_cxx_libraries = True,
    visibility = ["PUBLIC"],
    deps = [
        ":java",
        yoga_dep("testutil:java"),
        JUNIT_TARGET,
    ],
)

yoga_java_binary(
    name = "yoga",
    deps = [
        ":java",
    ],
)
